package com.incar.aiuidemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


import com.iflytek.aiui.AIUIConstant;
import com.iflytek.aiui.AIUIMessage;
import com.incar.aiuidemo.databinding.ActivityMainBinding;
import com.incar.services.AIUIListenerService;
import com.incar.services.AIUIservice;
import com.incar.util.FileUtil;


/**
 * 《错误码和解决方案》https://www.yuque.com/aiui/zzoolv/igbuol?singleDoc#
 * 开发文档 https://aiui.xfyun.cn/doc/aiui/3_access_service/access_interact/functions/voice_wakeup.html#%E9%94%99%E8%AF%AF%E7%A0%81%E6%9F%A5%E8%AF%A2
 * 关键词下载 https://aiui.xfyun.cn/app/94c273ea/download
 * */
public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_WRITE_STORAGE = 1;

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = new Intent(MainActivity.this, AIUIListenerService.class);
        startService(intent);
        // 检查并请求权限
        if (checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});

        }
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_STORAGE);

        } else {
            // 权限已授予，开始复制文件
            copyIVWFiles();
        }
        // 在Activity的onCreate中
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 隐藏导航栏（旧版方式）
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
            );
            // 沉浸式模式（隐藏导航栏并响应手势）
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    /**
     * 申请权限
     */
    private void requestPermissions(String[] perms) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ActivityCompat.requestPermissions(this, perms, 0x0010);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                            String[] permissions,
                                           int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_WRITE_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                copyIVWFiles();
            } else {
                Toast.makeText(this, "需要存储权限才能正常工作", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void copyIVWFiles() {
        // 调用文件复制方法
      //  FileUtil.copyAssetFolder("ivw", getApplicationContext().getExternalFilesDir(null)+"/sdcard/AIUI/ivw");
    }


    public void onclic(View view) {
        //sendBroadcast(new Intent("com.incar.aiuidemo"));
        //sendBroadcast(new Intent("com.incar.aiuidemo.star"));
        Log.d("tag", "onclic: --");
    }
}